$(function () {
    getStorageList()

    cheakprovince()
})

/**
 * 获得所有仓库信息
 */
function getStorageList() {
    $.ajax({
        type:"post",
        url:"/StoreManage/getStorageByCondition",
        data:{},
        dataType:"json",
        success:function(r){
            if (r.data != null){
                var t="";
                for (var i=0;i < r.data.length;i++){
                    t +=  "<tr storageid='"+r.data[i].id+"' >\n" +
                        "          <td>"+(i+1)+"</td>\n" +
                        "          <td>"+r.data[i].housename+"</td>\n" +
                        "          <td>"+r.data[i].principle+"</td>\n" +
                        "          <td>"+r.data[i].phone+"</td>\n" +
                        "          <td>"+r.data[i].houseaddress+r.data[i].houseaddresscity+"</td>\n" +
                        "          <td "+r.data[i].stateid+">"+r.data[i].state+"</td>\n" +
                        "          <td>"+r.data[i].createtime+"</td>\n" +
                        "          <td>"+r.data[i].createman+"</td>\n" +
                        "          <td>\n";
                    if(r.data[i].state==="可用"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" onclick=\"toStorageUpdateDetial(this)\">修改</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认注销此条信息？')\">注销</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="不可用"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认恢复此条信息？')\">恢复</a>\n" +
                            "          </td>\n";
                    }
                         t+="        </tr>"
                }
                $(".tablelist tbody").html(t);
                var pageNum = document.getElementById("pageNum");
                pageNum.innerHTML=r.pageNum
                var totalcomment = document.getElementById("totalcomment");
                totalcomment.innerHTML=r.totalRows
                totalPage=r.totalPage;
                getpagemodel(totalPage);
                getprovince();
            }
        },
        error:function (r) {
            alert("!!!")
        }

    })
}

/**
 * 取得省份信息
 */
function getprovince() {
    $.ajax({
        type:"post",
        url:"/StoreManage/getProvince",
        data:{},
        dataType:"json",
        success:function(r){
            if (r!= null){
                var t="<option>请选择省份</option>";
                for (var i=0;i < r.length;i++){
                    t += "<option>"+r[i].pname+"</option>\n"
                }
                $(".tools>li:nth-child(2)>select:first-child").html(t);
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

var totalPage;
//上一页
function shangyiye() {
    var pagenum = document.getElementById("pageNum");
    var n=pagenum.innerHTML
    if (n>1){
        n--;
        $.ajax({
            type:"post",
            url:"/StoreManage/getStorageByCondition",
            data:{pagenum:n},
            dataType:"json",
            success:function(r){
                if (r.data != null){
                    var t="";
                    for (var i=0;i < r.data.length;i++){
                        t +=  "<tr storageid='"+r.data[i].id+"' >\n" +
                            "          <td>"+(i+1)+"</td>\n" +
                            "          <td>"+r.data[i].housename+"</td>\n" +
                            "          <td>"+r.data[i].principle+"</td>\n" +
                            "          <td>"+r.data[i].phone+"</td>\n" +
                            "          <td>"+r.data[i].houseaddress+r.data[i].houseaddresscity+"</td>\n" +
                            "          <td "+r.data[i].stateid+">"+r.data[i].state+"</td>\n" +
                            "          <td>"+r.data[i].createtime+"</td>\n" +
                            "          <td>"+r.data[i].createman+"</td>\n" +
                            "          <td>\n";
                        if(r.data[i].state==="可用"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" onclick=\"toStorageUpdateDetial(this)\">修改</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认注销此条信息？')\">注销</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="不可用"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认恢复此条信息？')\">恢复</a>\n" +
                                "          </td>\n";
                        }
                        t+="        </tr>"
                    }
                    $(".tablelist tbody").html(t);
                    var pageNum = document.getElementById("pageNum");
                    pageNum.innerHTML=r.pageNum;
                    var totalcomment = document.getElementById("totalcomment");
                    totalcomment.innerHTML=r.totalRows;
                    totalPage=r.totalPage;
                    getpagemodel(totalPage);
                }
            },
            error:function (r) {
                alert("!!!")
            }

        })
    }else{
        alert("已经是第一页了")
    }
}
//下一页
function xiayiye() {
    var pagenum = document.getElementById("pageNum");
    var n=pagenum.innerHTML
    if (n<totalPage){
        n++;
        $.ajax({
            type:"post",
            url:"/StoreManage/getStorageByCondition",
            data:{pagenum:n},
            dataType:"json",
            success:function(r){
                if (r.data != null){
                    var t="";
                    for (var i=0;i < r.data.length;i++){
                        t +=  "<tr storageid='"+r.data[i].id+"' >\n" +
                            "          <td>"+(i+1)+"</td>\n" +
                            "          <td>"+r.data[i].housename+"</td>\n" +
                            "          <td>"+r.data[i].principle+"</td>\n" +
                            "          <td>"+r.data[i].phone+"</td>\n" +
                            "          <td>"+r.data[i].houseaddress+r.data[i].houseaddresscity+"</td>\n" +
                            "          <td "+r.data[i].stateid+">"+r.data[i].state+"</td>\n" +
                            "          <td>"+r.data[i].createtime+"</td>\n" +
                            "          <td>"+r.data[i].createman+"</td>\n" +
                            "          <td>\n";
                        if(r.data[i].state==="可用"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" onclick=\"toStorageUpdateDetial(this)\">修改</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认注销此条信息？')\">注销</a>\n" +
                                "          </td>\n";
                        }else if(r.data[i].state==="不可用"){
                            t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                                "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认恢复此条信息？')\">恢复</a>\n" +
                                "          </td>\n";
                        }
                        t+="        </tr>"
                    }
                    $(".tablelist tbody").html(t);
                    var pageNum = document.getElementById("pageNum");
                    pageNum.innerHTML=r.pageNum;
                    var totalcomment = document.getElementById("totalcomment");
                    totalcomment.innerHTML=r.totalRows;
                    totalPage=r.totalPage;
                    getpagemodel(totalPage);
                }
            },
            error:function (r) {
                alert("!!!")
            }

        })
    }else{
        alert("已经是最后一页了")
    }
}
//渲染页数
function getpagemodel(n) {
    var t="<li class=\"paginItem\"><a href=\"javascript:;\" onclick=\"shangyiye()\"><span class=\"pagepre\"></span></a></li>\n";
    for(var i=1;i<=n;i++){
        t+="<li class=\"paginItem\"><a href=\"javascript:;\" onclick='choiseModel(this)'>"+i+"</a></li>\n";
    }
    t+="<li class=\"paginItem\"><a href=\"javascript:;\" onclick=\"xiayiye()\"><span class=\"pagenxt\"></span></a></li>";
    $(".paginList").html(t);
}
//选择页码去到分页
function choiseModel(n) {
    var pagenum = $(n).html();
    $.ajax({
        type:"post",
        url:"/StoreManage/getStorageByCondition",
        data:{pagenum:pagenum},
        dataType:"json",
        success:function(r){
            if (r.data != null){
                var t="";
                for (var i=0;i < r.data.length;i++){
                    t +=  "<tr storageid='"+r.data[i].id+"' >\n" +
                        "          <td>"+(i+1)+"</td>\n" +
                        "          <td>"+r.data[i].housename+"</td>\n" +
                        "          <td>"+r.data[i].principle+"</td>\n" +
                        "          <td>"+r.data[i].phone+"</td>\n" +
                        "          <td>"+r.data[i].houseaddress+r.data[i].houseaddresscity+"</td>\n" +
                        "          <td "+r.data[i].stateid+">"+r.data[i].state+"</td>\n" +
                        "          <td>"+r.data[i].createtime+"</td>\n" +
                        "          <td>"+r.data[i].createman+"</td>\n" +
                        "          <td>\n";
                    if(r.data[i].state==="可用"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" onclick=\"toStorageUpdateDetial(this)\">修改</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认注销此条信息？')\">注销</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="不可用"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认恢复此条信息？')\">恢复</a>\n" +
                            "          </td>\n";
                    }
                    t+="        </tr>"
                }
                $(".tablelist tbody").html(t);
                var pageNum = document.getElementById("pageNum");
                pageNum.innerHTML=r.pageNum;
                var totalcomment = document.getElementById("totalcomment");
                totalcomment.innerHTML=r.totalRows;
                totalPage=r.totalPage;
                getpagemodel(totalPage);
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

/**
 * 根据省份确定市区
 */
function cheakprovince(){
    $(".tools>li:nth-child(2)>select:nth-child(1)").blur(function () {
        var r =$(".tools>li:nth-child(2)>select:first-child").val();
        if(r==="请选择省份"){
            var c="<option>请选择城市</option>";
            $(".tools>li:nth-child(2)>select:nth-child(2)").html(c);
        }else{
            $.ajax({
                type:"post",
                url:"/StoreManage/cheakprovince",
                data:{province:r},
                dataType:"json",
                success:function(r){
                    if (r!= null){
                        var q="<option>请选择城市</option>";
                        for (var i=0;i < r.length;i++){
                            q += "<option>"+r[i].cname+"</option>\n"
                        }
                        $(".tools>li:nth-child(2)>select:nth-child(2)").html(q);
                    }},
                error:function (r) {
                    alert("!!!")
                }
            })
        }
    })

}

//查询功能
function check() {
    var checkhouse =$(".tools>li:first-child>input:first-child").val();
    var checkaddress=$(".tools>li:nth-child(2)>select:first-child").val();
    var checkaddresscity=$(".tools>li:nth-child(2)>select:nth-child(2)").val();
    if(checkaddress=="请选择省份"){
        checkaddress ="";
    }
    if(checkaddresscity=="请选择城市"){
        checkaddresscity ="";
    }
    $.ajax({
        type:"post",
        url:"/StoreManage/checkStatic",
        data:{checkhouse:checkhouse,checkaddress:checkaddress,checkaddresscity:checkaddresscity},
        dataType:"json",
        success:function(r){
            if (r.data != null){
                var t="";
                for (var i=0;i < r.data.length;i++){
                    t +=  "<tr storageid='"+r.data[i].id+"' >\n" +
                        "          <td>"+(i+1)+"</td>\n" +
                        "          <td>"+r.data[i].housename+"</td>\n" +
                        "          <td>"+r.data[i].principle+"</td>\n" +
                        "          <td>"+r.data[i].phone+"</td>\n" +
                        "          <td>"+r.data[i].houseaddress+r.data[i].houseaddresscity+"</td>\n" +
                        "          <td "+r.data[i].stateid+">"+r.data[i].state+"</td>\n" +
                        "          <td>"+r.data[i].createtime+"</td>\n" +
                        "          <td>"+r.data[i].createman+"</td>\n" +
                        "          <td>\n";
                    if(r.data[i].state==="可用"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" onclick=\"toStorageUpdateDetial(this)\">修改</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认注销此条信息？')\">注销</a>\n" +
                            "          </td>\n";
                    }else if(r.data[i].state==="不可用"){
                        t+="          \t<a href=\"javascript:void(0)\" onclick=\"toStorageDetial(this)\">查看详情</a>\n" +
                            "            <a href=\"javascript:void(0)\" class=\"tablelink\" onclick=\"tipOpen(this,'是否确认恢复此条信息？')\">恢复</a>\n" +
                            "          </td>\n";
                    }
                    t+="        </tr>"
                }
                $(".tablelist tbody").html(t);
                var pageNum = document.getElementById("pageNum");
                pageNum.innerHTML=r.pageNum
                var totalcomment = document.getElementById("totalcomment");
                totalcomment.innerHTML=r.totalRows
                totalPage=r.totalPage;
                getpagemodel(totalPage);
                getprovince();
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

//去到仓库的详情
function toStorageDetial(n) {
    var storageid = $(n).parent().parent().attr("storageid");
    $.ajax({
        type:"post",
        url:"/StoreManage/toStorageDetial",
        data:{storageid:storageid},
        dataType:"json",
        success:function(r){
            if(r=="false"){
                alert("错!!!");
            }else{
                location.href="storageView.html";
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

//去到仓库的修改界面
function toStorageUpdateDetial(n) {
    var storageid = $(n).parent().parent().attr("storageid");
    $.ajax({
        type:"post",
        url:"/StoreManage/toStorageUpdateDetial",
        data:{storageid:storageid},
        dataType:"json",
        success:function(r){
            if(r=="false"){
                alert("错!!!");
            }else{
                location.href="storageUpdate.html";
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

/**
 * 打开提示框并且传入库存的id
 * @param n
 * @param content
 */
function tipOpen(n,content) {
    var storageid = $(n).parent().parent().attr("storageid");
    // var stateid = $(n).parent().parent().children("td[stateid]").attr("stateid");
    $(".tipright p").text(content);
    $("#tip").fadeIn(200);
    $.ajax({
        type:"post",
        url:"/StoreManage/tochangeStorageState",
        data:{storageid:storageid},
        dataType:"json",
        success:function(r){
            if(r=="false"){
                alert("错!!!");
            }
        },
        error:function (r) {
            alert("!!!")
        }
    })

}

/**
 * 关闭提示框的同时改变仓库状态
 */
function tipClose1() {
    $("#tip").fadeOut(200);
    $.ajax({
        type:"post",
        url:"/StoreManage/changeStorageState",
        dataType:"json",
        success:function(r){
            if(r=="false"){
                alert("错!!!");
            }
            location.href="storageList.html";
        },
        error:function (r) {
            alert("!!!")
        }
    })
}

function tipClose2() {
    $("#tip").fadeOut(200);
}